﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Windows.Forms.DataVisualization.Charting;
using TPLinkNetworking;

namespace HomeSuiteHome
{
    public partial class reporting : Form
    {
        private List<TPLinkConnection.SmartLink> _smartLinks = new List<TPLinkConnection.SmartLink>();
        public reporting(IEnumerable<Module> modules)
        {
            InitializeComponent();
            _smartLinks.AddRange(modules.Where(a => 
                                    a.moduleLink.ModuleInformation.system.get_sysinfo.dev_name != "Wi-Fi Smart Plug")
                                    .Select(b => b.moduleLink));
        }

        private void reporting_Load(object sender, EventArgs e)
        {
            tabControl1.TabPages.Clear();
            foreach (var smartLink in _smartLinks)
            {
                tabControl1.TabPages.Add(smartLink.ModuleInformation.system.get_sysinfo.deviceId,
                                         smartLink.ModuleInformation.system.get_sysinfo.alias);
            }
            foreach (var smartLink in _smartLinks)
            {
                var idOfCoorespondingTab =
                    tabControl1.TabPages.IndexOfKey(smartLink.ModuleInformation.system.get_sysinfo.deviceId);
                var correspondingTab = tabControl1.TabPages[idOfCoorespondingTab];

                //Generate the panel to hold data
                var framework = new SplitContainer
                {
                    Dock = DockStyle.Fill,
                    SplitterDistance = this.Width - 190,
                    IsSplitterFixed = true
                };

                var chart = new Chart {Dock = DockStyle.Fill};
                chart.Series.Clear();
                chart.Series.Add("Power Usage");
                chart.DataSource = smartLink.ModuleInformation.EnergyStats;

                if (framework.Panel1.Container == null) continue;
                framework.Panel1.Container.Add(chart);

                correspondingTab.Container?.Add(framework);
            }

        }
    }
}
